package org.leumasjaffe.initiativeTracker.view;


import java.awt.Color;
import java.awt.Graphics;

import javax.swing.JFrame;

import org.leumasjaffe.initiativeTracker.Constants;
import org.leumasjaffe.initiativeTracker.entity.Character;
import org.leumasjaffe.initiativeTracker.entity.CharacterList;
import org.leumasjaffe.initiativeTracker.operator.InitiativeController;
import org.leumasjaffe.initiativeTracker.operator.UnitManager;

/**
 * GUI class to visually interact with the initiative tracker data
 * 
 * @author Sam Jaffe
 * 
 */
public final class InitiativeFrame extends JFrame {
	private static final long serialVersionUID = 4386374088814720146L;

	static InitiativeFrame instance;

	private final CharacterList list;
	
	private final UnitManager root;

	private static final int drawDisp;
	
	static {
		if (System.getProperty("os.name").contains("Windows")) {
			drawDisp = 10;
		}
		else {
			drawDisp = 0;
		}
	}

	public static final int pageLength = 10;

	public static InitiativeFrame getInstance() {
		return instance;
	}

	/**
	 * Constructor for the DNDInitiativeGUI class
	 * 
	 * @param um
	 *            The UnitManager that is generating this instance
	 * @param isDM
	 *            A boolean indicating whether or not the user is Dungeon Master
	 */
	private InitiativeFrame(InitiativeController tracker) {
		super();
		this.list = tracker.list;
		this.root = tracker.root;
		Constants.pageNum = 0;
	}

	/**
	 * Sets the bounds on, adds to the panel, and adds action listeners for
	 * components of this GUI
	 */
	public void init() {
		setLayout(null);
		setTitle("DND Initiative Tracker");
		setSize(1000 + 3 * drawDisp, 580 + 3 * drawDisp);
		setLocation(0, 0);
		ControlPanel.getInstance().init();
		ControlPanel.getInstance().setBounds(0, 0, 1200, 50 + drawDisp);
		add(ControlPanel.getInstance());
		repaint();
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		setVisible(true);
	}

	@Override
	public void paint(Graphics g) {
		super.paint(g);

		g.setColor(Color.BLACK);
		for (int x = 0; x < (this.list.size() - (Constants.pageNum * pageLength))
				&& x < pageLength; x++) {
			int z = 126 + (50 * x) + drawDisp;
			g.drawLine(20, z, 820, z);
		}

		if (this.list.size() > 0) {
			int subTurn = this.root.getSubTurn();
			if (onPage(subTurn)) {
				Character c = this.list.get(subTurn).getCharacter();
				int n = (subTurn - (Constants.pageNum * pageLength))
						* 50 + 80 + drawDisp;
				g.fillRect(5, n, 10, 40);
				double d = ((double) c.getHp()) / ((double) c.getMaxHP());
				if (d <= 0.5) {
					g.setColor(Color.RED);
					g.fillRect(7, n + 5, 6, 30);
				}
			}
		}
	}

	/**
	 * Iterates through allPieces, adding Characters to the GUI that are on the
	 * page, and removing those that are not
	 */
	public void repage() {
		for (int x = 0; x < this.list.size(); x++) {
			if (onPage(x))
				this.list.get(x).update(x - (Constants.pageNum * pageLength));
			else
				this.list.get(x).tearDown();
		}
	}

	/**
	 * Checks if a given position in considered on the page
	 * 
	 * @param pos
	 *            The location data of the Character
	 * @return True if it is in the page's bounds, else false
	 */
	public static boolean onPage(int pos) {
		return pos >= (Constants.pageNum * pageLength)
				&& pos < ((Constants.pageNum + 1) * pageLength);
	}

	public void reset() {
		this.root.setTurnNum(1);
		this.root.setSubTurn(0);
		ControlPanel.getInstance().reset();
	}
	
	public void setUp() {		
		repage();
		repaint();
		validate();
		
		this.root.changeTurn();
	}

	/**
	 * @param tracker
	 */
	public static synchronized void load(InitiativeController tracker) {
		if (instance == null)
			instance = new InitiativeFrame(tracker);
	}

}
